package com.small.sentinel.slots.statistic.base;

public class WindowWrap<T> {
    /**
     * 窗口长度
     */
    private final long windowLengthInMs;

    /**
     * 窗口开始
     */
    private long windowStart;

    private T value;

    public WindowWrap(long windowLengthInMs, long windowStart, T value) {
        this.windowLengthInMs = windowLengthInMs;
        this.windowStart = windowStart;
        this.value = value;
    }
    public T value() {
        return value;
    }

    public long windowStart() {
        return windowStart;
    }

    public WindowWrap<T> resetTo(long startTime) {
        this.windowStart = startTime;
        return this;
    }

    /**
     * 检验时间是否在当前窗口内
     * 开始时间小于当前时间 并且 当前时间在窗口间隔内（当前时间小于开始时间+时间间隔）
     * @param timeMillis 检验时间
     * @return 如果给定时间在当前存储桶中，则为true，否则为false
     */
    public boolean isTimeInWindow(long timeMillis) {
        return windowStart <= timeMillis && timeMillis < windowStart + windowLengthInMs;
    }

}
